import scrapy

from scrapy_05_movie.items import Scrapy05MovieItem


class MvSpider(scrapy.Spider):
    name = "mv"
    allowed_domains = ["www.dygod.net"]
    start_urls = ["https://www.dygod.net/html/gndy/china/index.html"]

    def parse(self, response):
        print('===================================')
        # 第一页的名字, 第二页的图片
        a_list = response.xpath('//div[@class="co_content8"]//td[2]//a[2]')
        for a in a_list:
            # 获取第一页的name和要点击的链接
            name = a.xpath('./text()').extract_first()
            href = a.xpath('./@href').extract_first()

            # 第二页的地址是
            url = "https://www.dygod.net" + href

            # 对第二页的链接发起访问
            yield scrapy.Request(url=url, callback=self.parse_second, meta={'name': name})

    def parse_second(self, response):
        # 注意如果拿不到数据的情况下一定检查你的xpath语法是否正确
        src = response.xpath('//div[@id="Zoom"]/img[1]/@src').extract_first()
        src = 'https://www.dygod.net' + src

        # 接受到请求的那个meta参数的值
        name = response.meta['name']

        movie = Scrapy05MovieItem(src=src, name=name)
        yield movie
